package jianzhiOffer;

import java.util.Deque;
import java.util.LinkedList;

public class Num016 {
    public int lengthOfLongestSubstring(String s) {
        if(s.length() == 0) return 0;
        int maxlen = 1;
        int count = 1;
        char[] chars = s.toCharArray();
        int l = 0,r = 1;
        Deque<Character> queue = new LinkedList<>();
        queue.offer(chars[l]);
        while(r < chars.length) {
            if(!queue.contains(chars[r])) {
                queue.addLast(chars[r]);
                count++;
                r++;
            }else {
                maxlen = Math.max(maxlen,count);
                while(chars[l] != queue.peek()) {
                    queue.pollFirst();
                    count--;
                    l++;
                }
                queue.pollFirst();
                count--;
                l++;
            }
        }
        maxlen = Math.max(maxlen,count);
        return maxlen;
    }
}
